package diona.snippet.requests

import net.liftweb.util.BindHelpers._
import net.liftweb.http.{SHtml, StatefulSnippet, S}
import diona.search.SearchRequest
import diona.model.User

/**
 * User: anton
 * Date: 25.07.11 21:38
 */
class AddSearchRequest extends StatefulSnippet{
  private var query = ""
  private val whence = S.referer openOr "/"

  def saveQuery(query: String) = {
    SearchRequest.getOrCreateByQuery(User.currentUser.open_!, query)
  }

  def submit() = {
    saveQuery(query)
    S.redirectTo(whence)
  }

  def dispatch = {case "render" => render}

  def render = {
    "name=query" #> SHtml.text(query, query =_, "id" -> "search_query") &
    "type=submit" #> SHtml.onSubmitUnit(submit _)
  }
}